package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.*;
import com.ys.enume.storage.TccFbaLogisticsMethodEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/web/storage/tccFbaFeeDetails")
public class WebTccFbaFeeDetailsController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccFbaFeeDetailsController.class);

//	@Autowired
//	private CommonApiService<TccFbaPlan> commonApiService;
//
//	private final Long MENU_ID = MenuId.TCC_FbaFeeDetails;
//
//	@Autowired
//	private CommonApiService<TccFbaLogisticsMethod> commonApiServiceWl;
//
//	@Autowired
//	private CommonApiService<TccFbaFeeDetails> commonApiServiceFee;
//
//	private final Long MENU_IDWl = MenuId.TCC_FbaLogisticsMethodJh;
//
//	private final Long MENU_IDFee = MenuId.TCC_FbaFeeDetailsJh;

	private final Long MENU_ID = MenuId.TCC_FbaPlan;

	private final Long MENU_IDWl = MenuId.TCC_FbaLogisticsMethodJh;

	private final Long MENU_IDFee = MenuId.TCC_FbaFeeDetailsJh;

	@Autowired
	private CommonApiService<TccFbaPlan> commonApiService;

	@Autowired
	private CommonApiService<TccFbaLogisticsMethod> commonApiServiceWl;

	@Autowired
	private CommonApiService<TccFbaFeeDetails> commonApiServiceFee;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response, String goodsIds) {
		try {
			TccFbaPlan entity = new TccFbaPlan();
			String bid = commonApiService.getCommonNextBid(MENU_ID);
			entity.setBid(bid);
			entity.setCreateTime(new Date());

			Map<String, Object> extraParam = new HashMap<>();
			//关联数据
			extraParam.put("declarationStateName", "空");
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,new ArrayList<>());
			commonApiService.toAdd(model, entity, MENU_ID, extraParam, subGridConfig);
			return "storage/tccFbaPlan/toAdd";
		}catch (Exception e) {
			logger.error("详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
		try {
			Map<String, Object> tccFbaPlan = new TccFbaPlan().setBid(bid)
					.addLeftJoin("YS_ProcInst P ON P.Bid = A.Bid ")
					.addField("FORMAT(A.createTime, 'yyyy-MM-dd HH:mm:ss') createTime1")//创建时间
					.addField("CASE WHEN  A.EffectStock = 1 THEN FORMAT(A.UpdateTime, 'yyyy-MM-dd HH:mm:ss') ELSE FORMAT(P.UpdateTime, 'yyyy-MM-dd HH:mm:ss') END auditingTime")//审核时间
					.addField("case when A.declarationState = 0 then '否' else '是' end as declarationStateName")
					.addLeftJoin("TCC_FbaLogisticsMethod C ON C.sourceBid = A.bid AND C.sourceType = 0")

					.addField("C.logsticsWay,C.cck,C.vck,C.estimatedDay,C.transportationCn transportationTypeCn,C.logsticsWayId,C.remark logsticsRemark")
					.addField("case when C.stateShareTicket = 1 then '是' else '否' end as stateShareTicketName")//是否拼票
					.addField("case when C.nameLogsticsClearance = 0 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
					.addLeftJoin("TCC_FbaFeeDetails D ON D.sourceBid = A.bid AND D.sourceType = 0")
					.addField("D.priceTranPredict,D.priceTranCalWeight,D.priceExtraPredict")
					.queryFirstMap();
			List<Map<String, Object>> itemList = new TccFbaPlanItem()
					.addField("(CONVERT(varchar(19), cast(A.lengthPackage as float)) + '*' + CONVERT(varchar(19), cast(A.widthPackage as float))  + '*' + CONVERT(varchar(19), cast(A.heightPackage as float))) packageSize")
					.addField("case when A.declarationItemState = 0 then '否' else '是' end as declarationItemStateName")
					.setBidFbaPlan(bid).queryMap();
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,itemList);

			commonApiService.toEdit(model, Long.valueOf(tccFbaPlan.get("id").toString()), new TccFbaPlan(), MENU_ID, tccFbaPlan, subGridConfig);
			return "storage/tccFbaPlan/toEdit";
		}catch (Exception e) {
			logger.error("编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 预估物流方式详情- 详情
	 */
	@GetMapping("/toEditWl")
	public String toEditWl(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
		try {
			Map<String, Object> tccFbaLogisticsMethod = new TccFbaLogisticsMethod().setSourceBid(bid)

					.addField("case when A.stateShareTicket = 1 then '是' else '否' end as stateShareTicketName")//是否拼票
					.addField("case when A.nameLogsticsClearance = 0 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
					.addField("case when A.divisionalRule = 0 then '0.1kg进1kg' when A.divisionalRule = 1 then '0.5kg进1kg' else '不约分' end as divisionalRuleName")//约分规则
					.addField("A.SourceBid bid")
					.addField("FORMAT(A.createTime, 'yyyy-MM-dd HH:mm:ss') createTime1")//创建时间
					.addLeftJoin("TCC_FbaPlan tfp ON tfp.Bid=A.SourceBid")
					.addLeftJoin("YS_ProcInst P ON P.Bid = tfp.Bid ")
					.addField("tfp.Vgsbm vgsbm,tfp.VgsJc vgsJc")
					.setSourceType(TccFbaLogisticsMethodEnum.SourceType.FBAPLAN.value).queryFirstMap();
			commonApiServiceWl.toEdit(model, Long.valueOf(tccFbaLogisticsMethod.get("id").toString()), new TccFbaLogisticsMethod(), MENU_IDWl, tccFbaLogisticsMethod, null);
			return "storage/tccFbaFeeDetails/toEditWl";
		}catch (Exception e) {
			logger.error("物流方式详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 实际物流方式详情 - 详情
	 */
	@GetMapping("/toEditWl2")
	public String toEditWl2(Model model, HttpServletRequest request, HttpServletResponse response, String sourceBid) {
		try {
			Map<String, Object> tccFbaLogisticsMethod = new TccFbaLogisticsMethod().setSourceBid(sourceBid)
					.addField("case when A.nameLogsticsClearance = 1 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
					.addField("case when A.divisionalRule = 0 then '0.1kg进1kg' when A.divisionalRule = 1 then '0.5kg进1kg' else '不约分' end as divisionalRuleName")//约分规则
					.addField("A.SourceBid bid")
					.addField("FORMAT(A.createTime, 'yyyy-MM-dd HH:mm:ss') createTime1")//创建时间
					.addLeftJoin("TCC_FbaPlan tfp ON tfp.Bid=A.SourceBid")
					.addLeftJoin("YS_ProcInst P ON P.Bid = tfp.Bid ")
					.addField("tfp.Vgsbm vgsbm,tfp.VgsJc vgsJc")
					.setSourceType(TccFbaLogisticsMethodEnum.SourceType.FBAFH.value).queryFirstMap();
			commonApiServiceWl.toEdit(model, Long.valueOf(tccFbaLogisticsMethod.get("id").toString()), new TccFbaLogisticsMethod(), MENU_IDWl, tccFbaLogisticsMethod, null);
			return "storage/tccFbaFeeDetails/toEditWl";
		}catch (Exception e) {
			logger.error("物流方式详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}




	/*
	 * 预估费用详情
	 */
	@GetMapping("/toEditFeeYG")
	public String toEditFeeYG(Model model, HttpServletRequest request, HttpServletResponse response, String sourceBid) {
		try {
			Map<String, Object> tccFbaFeeDetails = new TccFbaFeeDetails().setSourceBid(sourceBid)
					.setSourceType(TccFbaLogisticsMethodEnum.SourceType.FBAPLAN.value)
					.addLeftJoin("TCC_FbaPlan B ON B.Bid = A.SourceBid AND A.SourceType = 0")
					.addField("B.bid bid,B.Vgsbm vgsbm,B.VgsJc vgsJc,B.ShopName shopName")
					.addLeftJoin("YS_ProcInst P ON P.Bid = B.Bid ")
					.addField("CASE WHEN P.UpdateTime IS NULL AND B.EffectStock = 1 THEN FORMAT(B.UpdateTime, 'yyyy-MM-dd HH:mm:ss') ELSE FORMAT(P.UpdateTime, 'yyyy-MM-dd HH:mm:ss') END auditingTime")//审核时间
					.addLeftJoin("TCC_FbaLogisticsMethod C ON C.SourceBid = B.Bid AND C.SourceType = 0")
					.addField("C.LogsticsWay logsticsWay,C.Cck cck,C.nameLogsticsClearance nameLogsticsClearance")
					.addField("C.transportationCn transportationCn,C.remark remarkWl,C.vck vck,C.clearanceOpeAmount clearanceOpeAmount,C.jfFee jfFee")
					.addField("C.pzFee pzFee")
					.addField("case when (A.numProName - C.FreeProNameNum) < 0 then 0 else A.numProName - C.FreeProNameNum end as numProNameSf")

					.addField("case when C.stateShareTicket = 1 then '是' else '否' end as stateShareTicketName")//是否拼票
					.addField("case when C.nameLogsticsClearance = 0 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
					.addField("CAST('MAX[MAX(实重,抛重)+约分规则，单票起重，单票起重]*（计重梯度价+抛重附加费）+预估物流附加费(￥)' AS VARCHAR(255))  priceTranPredictSm")
					.addField("CAST('预估品名附加费(￥)+预估材质附加费(￥)+清关服务费(￥)' AS VARCHAR(255))  priceExtraPredictSm")
					.queryFirstMap();
			commonApiServiceFee.toEdit(model, Long.valueOf(tccFbaFeeDetails.get("id").toString()), new TccFbaFeeDetails(), MENU_IDFee, tccFbaFeeDetails, null);
			return "storage/tccFbaFeeDetails/toEditWl";
		}catch (Exception e) {
			logger.error("预估费用详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 实际费用详情
	 */
	@GetMapping("/toEditFeeSJ")
	public String toEditFeeSJ(Model model, HttpServletRequest request, HttpServletResponse response, String sourceBid) {
		try {
			Map<String, Object> tccFbaFeeDetails = new TccFbaFeeDetails().setSourceBid(sourceBid)
					.setSourceType(TccFbaLogisticsMethodEnum.SourceType.FBAFH.value)
					.addLeftJoin("TCC_FbaPlan B ON B.Bid = A.SourceBid AND A.SourceType = 1")
					.addField("B.bid bid,B.Vgsbm vgsbm,B.VgsJc vgsJc,B.ShopName shopName")
					.addLeftJoin("YS_ProcInst P ON P.Bid = B.Bid ")
					.addField("CASE WHEN P.UpdateTime IS NULL AND B.EffectStock = 1 THEN FORMAT(B.UpdateTime, 'yyyy-MM-dd HH:mm:ss') ELSE FORMAT(P.UpdateTime, 'yyyy-MM-dd HH:mm:ss') END auditingTime")//审核时间
					.addLeftJoin("TCC_FbaLogisticsMethod C ON C.SourceBid = B.Bid AND C.SourceType = 1")
					.addField("C.LogsticsWay logsticsWay,C.Cck cck,C.nameLogsticsClearance nameLogsticsClearance")
					.addField("C.transportationCn transportationCn,C.remark remarkWl,C.vck vck,C.clearanceOpeAmount clearanceOpeAmount,C.jfFee jfFee")
					.addField("C.pzFee pzFee")
					.addField("case when (A.numProName - C.FreeProNameNum) < 0 then 0 else A.numProName - C.FreeProNameNum end as numProNameSf")
					.addField("case when C.nameLogsticsClearance = 1 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
					.addField("CAST('MAX[MAX(实重,抛重)+约分规则，单票起重，单票起重]*（计重梯度价+抛重附加费）+实际物流附加费(￥)' AS VARCHAR(255))  priceTranPredictSm")
					.addField("CAST('实际品名附加费(￥)+实际材质附加费(￥)+清关服务费(￥)' AS VARCHAR(255))  priceExtraPredictSm")
					.queryFirstMap();
			commonApiServiceFee.toEdit(model, Long.valueOf(tccFbaFeeDetails.get("id").toString()), new TccFbaFeeDetails(), MENU_IDFee, tccFbaFeeDetails, null);
			return "storage/tccFbaFeeDetails/toEditWl";
		}catch (Exception e) {
			logger.error("实际费用详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}


}




